<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19
 * Time: 14:28
 */

namespace App\Http\Controllers\Article;

use App\Model\CommentModel;
use App\Model\UserInfoModel;
use App\Model\ZanModel;
use Illuminate\Support\Facades\View;
use App\Http\Controllers\WebController;
use App\Model\ArticleModel;
use App\Model\Comment;
use App\Model\PhotoModel;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ArticleController extends WebController
{
    public function show(Request $request, $id)
    {
        if (is_numeric($id)) {
            $id = (int)$id;
            $data['list'] = ArticleModel::leftJoin('xf_userinfo', 'xf_userinfo.user_id', '=',
                'xf_article.user_id')->where('xf_article.article_id', $id)->first();
            $data['comment'] = CommentModel::leftJoin('xf_userinfo', 'xf_userinfo.user_id', '=',
                'xf_comment.user_id')->where('article_id', $id)->orderBy('xf_comment.add_time', 'desc')->get();
            $data['zan'] = ZanModel::where('article_id', $id)->count();
        } else {
            return back()->with('success', '非法操作');
        }
        return view('article/index', $data);
    }

    public function destroy($id)
    {
        CommentModel::where('article_id', $id)->delete();
        $id = ArticleModel::where('article_id', $id)->delete();
        if ($id) {
            back()->with('success', '删除文章成功');
            return redirect('/');
        } else {
            return back()->with('success', '删除文章失败');
        }
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $userId=session('userid');
        $userInfo = UserInfoModel::where('user_id', $userId)->first();
        if(!$userInfo){
            back()->with('success', '你还没有登录！请先登录再评论！');
            return redirect('/login');
        }
        if(strlen($data['comment'])<1){
            return back()->with('success', '请添写评论内容！');
        }
        DB::beginTransaction();
        try {
            $id = CommentModel::create([
                'content' => $data['comment'],
                'article_id' => $data['id'],
                'add_time' => date('Y-m-d H:i:s'),
                'user_id' => session('userid'),
            ]);
            DB::commit();
            if ($id) {
                return back()->with('success', '评论文章成功！');
            } else {
                return back()->with('success', '评论文章失败！');
            }
        } catch (\Exception $e) {
            DB::rollBack();
            Log::info("评论文章成功！" . $e->getTraceAsString());
        }

    }

    public function zan(Request $request)
    {
        $userId=session('userid');
        if(!$userId){
            return response()->json(array(
                'status' => 'login',
                'val' => '请先登录！',
            ));
        }
        $articleId = $request->get('article_id');
        $userInfo = ZanModel::where('article_id', $articleId)->where('user_id', $userId)->first();
        if ($userInfo) {
            ZanModel::where('article_id', $articleId)->where('user_id', $userId)->delete();
            $count = ZanModel::where('article_id', $articleId)->count();
            return response()->json(array(
                'status' => 'false',
                'val' => $count,
            ));
        }
        DB::beginTransaction();
        try {
            ZanModel::create([
                'article_id' => (int)$articleId,
                'user_id' => $userId,
                'add_time' => date('Y-m-d H:i:s')
            ]);
            DB::commit();
            $count = ZanModel::where('article_id', $articleId)->count();
            if ($count) {
                return response()->json(array(
                    'status' => 'true',
                    'val' => $count,
                ));
            }
        } catch (\Exception $e) {
            DB::rollBack();
            Log::info("文章点赞失败" . $e->getTraceAsString());
        }
    }
}